<?php
  require_once("db_connect.php");

  $id = isset($_GET['id'])?$_GET['id']:0;
  $updateCountryId = "";
  //pr($_REQUEST);
  ////Delete Country ////
  if(isset($_GET['mode']) && $_GET['mode']=='delete'){
    $delCountryQuery = "DELETE FROM `countries` WHERE `id`=$id";
    mysql_query($delCountryQuery)or die(mysql_error());
    $deletedRows = mysql_affected_rows();
    if($deletedRows == 1){
      echo "<h4>Country Deleted</h4>";
    }
  }
  ////Update Country ////
  if(isset($_GET['mode']) && $_GET['mode']=='edit'){
    $selCountryQuery = "SELECT * FROM `countries` WHERE `id`=$id";
    $resultCountryQuery = mysql_query($selCountryQuery)or die(mysql_error());
    $ansCountryQuery =  mysql_fetch_assoc($resultCountryQuery);
    $updateCountryId = $ansCountryQuery['id'];
    $updateCountryName = $ansCountryQuery['name'];
    //pr($ansCountryQuery);

  }

  // Update after post
  if(isset($_POST['name']) && !empty($_POST['id'])) {
    $postedId = $_POST['id'];
    $updatedName = $_POST['name'];
    $updateCountryQuery = "UPDATE `countries` SET `name`='$updatedName' WHERE `id`='$postedId'";

    $resultCountryQuery = mysql_query($updateCountryQuery)or die(mysql_error());
    if(mysql_affected_rows() == 1){
      echo "<h4>Country Updated</h4>";
    }
    //pr($updateCountryQuery);
  }

  //// Save Country ////
  if(isset($_POST['name']) && empty($_POST['id'])) {
    //echo "Yes POsted";
    $name = $_POST['name'];
    $query = "INSERT INTO `countries`(`name`) VALUES('$name')";
    mysql_query($query) or die(mysql_error());
    $rowsAffected = mysql_affected_rows();
    if($rowsAffected == 1){
      echo "<strong>$name</strong> has been added..";
    }
  }

  ////////////////////PAGING START ////////////////////////////////
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
$rpp = 2;//record_per_page

/*-`-`-`-`-`-`-`-`-`-`-`-`-``-`-`-`-`-`-`*/
$totalRecordQuery = "SELECT COUNT(*) as `total` FROM `countries`";
$totalRecordResult  = mysql_query($totalRecordQuery)or die(mysql_error());

$totalRecordAns = mysql_fetch_assoc($totalRecordResult);
/*-`-`-`-`-`-`-`-`-`-`-`-`-``-`-`-`-`-`-`*/
$totalRecords = $totalRecordAns['total'];
$totalPages = ceil($totalRecords / $rpp);
$index = ($current_page - 1)*$rpp;

  ////////////////////PAGING END ////////////////////////////////
  $selectCountryQuery = "SELECT * FROM `countries` LIMIT $index , $rpp";
  $selectCountryResult = mysql_query($selectCountryQuery) or die(mysql_error());

//var_dump($selectCountryResult);
echo "Total Countries :".$totalRecords;
?>
<form name="add_country" method="post" action="">
  <input type="hidden" name="id" value="<?=$updateCountryId?>">
<table border="1" align="center" width="40%">

  <thead>
    <tr>
      <th colspan="2">Add Country</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Country Name :</td>
      <td><input required type="text" name="name"
value="<?=@$updateCountryName?>"></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="submit" value="Add me"></td>
    </tr>
  </tbody>
</table>
</form>

<table border="1" align="center" width="40%">

  <thead>
    <tr>
      <th colspan="2">Countries </th>
      <th >Action </th>
    </tr>
  </thead>

  <tbody>
<?php
while($selectCountryAns = mysql_fetch_assoc($selectCountryResult)){
$id = $selectCountryAns['id'];
 // pr($selectCountryAns);
?>
    <tr>
      <td><?=$id?></td>
      <td><?=$selectCountryAns['name']?></td>
      <td align="center">
<!---- For delete ---->
<a onClick="return confirm('Are you sure?')"
href="add_country.php?id=<?=$id?>&mode=delete">Delete</a>
<!---- For Edit ---->

<a
href="add_country.php?id=<?=$id?>&mode=edit">Edit</a>

</td>
    </tr>
<?php
}//end while
?>
  </tbody>
</table>

<table border="1" align="center" width="40%">  <tr>
<?php for($i=1 ; $i<=$totalPages ; $i++){  ?>
    <td align="center"><a href="add_country.php?page=<?=$i?>"><?=$i?></a></td>
<?php }?>
  </tr>
<table>
